<template>
  <view class="member-container">
    <my-navbar title="我的"></my-navbar>

    <view class="user-info">
      <u-avatar :src="info.user.avatar_url" size="80"></u-avatar>
      <view class="user-detail">
        <text class="username">{{
          info.user.nickname || info.user.username
        }}</text>
        <text class="agent-name">{{ info.name }}</text>
      </view>
    </view>

    <view class="info-card">
      <view class="info-item">
        <text class="label">佣金比例</text>
        <text class="value">
          {{ (info.agent_commission * 100).toFixed(2) }}%
        </text>
      </view>
      <view class="info-item">
        <text class="label">代理区域</text>
        <text class="value btn" @click="handleOpenSwitchArea">{{
          info.city.full_name
        }}</text>
      </view>
      <view class="info-item">
        <text class="label">邀请供应商</text>
        <text class="value btn" @click="handlePreviewSupplierCode">查看</text>
      </view>
      <view class="info-item">
        <text class="label">邀请商户</text>
        <text class="value btn" @click="handlePreviewMerchantCode">查看</text>
      </view>
    </view>

    <view class="menu-list">
      <u-cell-group>
        <u-cell
          title="我的商户"
          icon="account"
          isLink
          @click="toMerchant"
        ></u-cell>
        <u-cell
          title="我的供应商"
          icon="account-fill"
          isLink
          @click="toSupplier"
        ></u-cell>
        <u-cell
          title="我的银行卡"
          icon="photo"
          isLink
          @click="toBankList"
        ></u-cell>
        <u-cell
          title="佣金明细"
          icon="rmb-circle"
          isLink
          @click="toCommission"
        ></u-cell>
        <u-cell
          title="提现管理"
          icon="rmb-circle"
          isLink
          @click="toWith"
        ></u-cell>
      </u-cell-group>
    </view>

    <view style="margin-top: 20rpx">
      <u-button type="error" @click="handleLogout">退出登录</u-button>
    </view>

    <up-modal
      :show="supplierCodeShow"
      title="邀请供应商"
      @confirm="supplierCodeShow = false"
    >
      <up-qrcode :size="200" :val="supplierCodeUrl"></up-qrcode>
    </up-modal>

    <up-modal
      :show="merchantCodeShow"
      title="邀请商户"
      @confirm="merchantCodeShow = false"
    >
      <u-image :src="merchantCodeUrl" mode="aspectFit"></u-image>
    </up-modal>

    <SwitchForm
      :show="switchAreaShow"
      @cancel="switchAreaShow = false"
      @confirm="switchAreaShow = false"
    ></SwitchForm>

    <my-tabbar current-page="/pages/agenter/member"></my-tabbar>
  </view>
</template>

<script setup>
import SwitchForm from "./components/switch-form.vue";
import { wxMiniProgramQRCode, switchAgentArea } from "@/common/apis/agent.js";
import { onShow } from "@dcloudio/uni-app";
import { computed, ref } from "vue";
import { useStore } from "vuex";

const store = useStore();

const info = computed(() => store.state.user.agentInfo);

const supplierCodeShow = ref(false);

const supplierCodeUrl = ref("");

const merchantCodeShow = ref(false);

const merchantCodeUrl = ref("");

const switchAreaShow = ref(false);

function handlePreviewSupplierCode() {
  supplierCodeUrl.value =
    import.meta.env.MODE === "development"
      ? "http://192.168.2.18:3000/#/login"
      : "http://supplier.zkodoo.cc/#/login";
  supplierCodeShow.value = true;
}

async function handlePreviewMerchantCode() {
  const agentId = store.state.user.agentInfo?.id;
  const agentCode = store.state.user.agentInfo?.invitation_code;
  const response = await wxMiniProgramQRCode({
    path: "pages/app/store",
    env: import.meta.env.MODE == "development" ? "develop" : "release",
    param_key: "agent",
    param_value: agentId,
  });
  if (response.data) {
    merchantCodeUrl.value = `data:image/png;base64,${response.data}`;
    merchantCodeShow.value = true;
  }
}

function handleLogout() {
  uni.showModal({
    title: "提示",
    content: "确定要退出登录吗？",
    success: (res) => {
      if (res.confirm) {
        store.dispatch("logout");
      }
    },
  });
}

function handleOpenSwitchArea() {
  switchAreaShow.value = true;
}

async function handleConfirm(values) {
  const response = await switchAgentArea({});
}

function toMerchant() {
  uni.navigateTo({
    url: "/pages/agenter/merchant",
  });
}

function toSupplier() {
  uni.navigateTo({
    url: "/pages/agenter/supplier",
  });
}

function toCommission() {
  uni.navigateTo({
    url: "/pages/agenter/commission",
  });
}

function toWith() {
  uni.navigateTo({
    url: "/pages/supplier/withdraw",
  });
}

function toBankList() {
  uni.navigateTo({
    url: "/pages/user/bank/list",
  });
}

onShow(() => {});
</script>

<style lang="scss" scoped>
.member-container {
  padding: 20rpx;

  .user-info {
    display: flex;
    align-items: center;
    padding: 30rpx;
    background: #fff;
    border-radius: 16rpx;
    margin-bottom: 20rpx;

    .user-detail {
      margin-left: 20rpx;
      display: flex;
      flex-direction: column;

      .username {
        font-size: 32rpx;
        font-weight: bold;
      }

      .agent-name {
        font-size: 28rpx;
        color: #666;
        margin-top: 10rpx;
      }
    }
  }

  .info-card {
    background: #fff;
    border-radius: 16rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;

    .info-item {
      display: flex;
      justify-content: space-between;
      padding: 20rpx 0;
      border-bottom: 1rpx solid #f5f5f5;

      &:last-child {
        border-bottom: none;
      }

      .label {
        color: #999;
      }

      .value {
        color: #333;

        &.btn {
          color: $u-primary;
          cursor: pointer;
        }
      }
    }
  }

  .menu-list {
    background: #fff;
    border-radius: 16rpx;
  }
}
</style>
